<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

include "database.php";
include "global.php";



function showAll($order){
    $sql = "SELECT * FROM product  ".$order;
   // echo $sql;
    $product = mysql_query($sql);


    return $product;
}

function showAllType($product_id){

    $sql = "SELECT * FROM product_type WHERE product_id=".$product_id." ORDER BY ratio ";
    $data ;
    $query = mysql_query($sql);
    
    return $query;
}

function addProduct($getItem){

  
    extract($getItem);
    $product_id = product_id_check($product_id);
    check_type($getItem);

    
    $sql="INSERT INTO product(product_id,name,amount) VALUES('".$product_id."','".$name."',0);";
    $query = mysql_query($sql);
    if(!$query)
        echo "Insert Product error";
    $newId=mysql_insert_id();
    insert_type($newId,$getItem);
    
}
function check_type($get){
    extract($get);
    $ckType=0;
    for($i = 1 ; $i < count($type_name) ; $i++){
        if(!empty($type_name[$i]) && !empty($type_nameunit[$i]) && !empty($type_ratio[$i]) && !empty($type_asset[$i]) && !empty($type_buyprice[$i]))
            $ckType++;
    }
    if($ckType < 0){
        echo "ต้องใส่ประเถทการขายอย่างน้อย1";
        exit();
    }

}
function insert_type($id,$get){
      extract($get);
     for($i = 1 ; $i < count($type_name) ; $i++){
        $sql =  "INSERT INTO product_type(product_id,name,nameunit,asset,ratio,buyprice) VALUES(".$id.",'".$type_name[$i]."','".$type_nameunit[$i]."',".$type_ratio[$i].",".$type_asset[$i].",".$type_buyprice[$i]."); ";
        $query = mysql_query($sql);
        if(!$query){
            echo $sql ;
            echo "Insert Product_type error";
        }
    }
}
function insert_type_id($id,$get){
      extract($get);
     for($i = 1 ; $i < count($type_name) ; $i++){
         if($type_id[$i]=="")
             $sql =  "INSERT INTO product_type(product_id,name,nameunit,asset,ratio,buyprice) VALUES(".$id.",'".$type_name[$i]."','".$type_nameunit[$i]."',".$type_ratio[$i].",".$type_asset[$i].",".$type_buyprice[$i]."); ";
         else
            $sql =  "INSERT INTO product_type(id,product_id,name,nameunit,asset,ratio,buyprice) VALUES(".$type_id[$i].",".$id.",'".$type_name[$i]."','".$type_nameunit[$i]."',".$type_ratio[$i].",".$type_asset[$i].",".$type_buyprice[$i]."); ";
        $query = mysql_query($sql);
        if(!$query){
            echo $sql ;
            echo "Insert Product_type error";
        }
    }
}
function query_one($id){
    $sql="SELECT * FROM product WHERE id=".$id;
    $query = mysql_query($sql);
    $output = mysql_fetch_array($query);
    return $output;
}

function query_one_type($id){
    $sql="SELECT * FROM product_type WHERE product_id=".$id;
    $query = mysql_query($sql);
    
    return $query;
}

function editProduct($get){
    extract($get);
    check_type($get);
    $sql="UPDATE product SET product_id='".$product_id."',name='".$name."' WHERE id=".$id;
    if(!mysql_query($sql))
        echo "update error";
    delete_type($id);
    insert_type_id($id,$get);
}

function delete($id){
    $sql="DELETE FROM product WHERE id=".$id;
    if(!mysql_query($sql)){
            echo "Delete Product error";
            exit();
    }
    delete_type($id);
    
}

function delete_type($id){
    $sql="DELETE FROM product_type WHERE product_id=".$id;
    if(!mysql_query($sql))
            echo "Delete Product_type error";
}
?>
